package com.lkm.app_ui;


import androidx.fragment.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lkm.app_ui.help.LockKeyManager;
import com.lkm.app_ui.m.fun.FunModule;
import com.lkm.comlib.ui.BaseFragment;
import com.lkm.frame.task2.TaskCollection;
import com.lkm.frame.task2.TaskCollectionCycle;
import com.lkm.javatool.lang.RunnablePR;

import java.util.ArrayDeque;
import java.util.Queue;

import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;



public abstract class MBaseFragment extends BaseFragment {

    protected Fragment targetFragment;
    private Integer requestCode = null;

    public void set(Fragment targetFragment, int requestCode) {
        this.targetFragment = targetFragment;
        this.requestCode = requestCode;
    }

    public void respondActivityResult(int resultCode, Intent data) {
        targetFragment.onActivityResult(requestCode, RESULT_OK, data);
        requestCode = null;
    }

    @Override
    public void onDestroy() {
        if(requestCode!=null){
            targetFragment.onActivityResult(requestCode, RESULT_CANCELED, null);
        }
        if(taskCollectionCycle!=null)taskCollectionCycle.cancelAllTask();
        super.onDestroy();

    }

    TaskCollectionCycle taskCollectionCycle;

    public TaskCollection holdTaskCollection() {
        if (taskCollectionCycle == null)
            taskCollectionCycle = new TaskCollectionCycle(AppConst.getTaskManager2(), holdCycleHelp());
        return taskCollectionCycle;
    }

    protected abstract boolean configIsLockKey();



    protected boolean configIsPassLockKey(){
return false;
    }

    RunnablePR<Void, Boolean> isLockRun = new RunnablePR<Void, Boolean>() {
        @Override
        public Boolean run(Void aVoid) {
            return configIsLockKey();
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(!configIsPassLockKey()) {
            LockKeyManager.addPage(isLockRun);
            LockKeyManager.curPageLockChange();
        }
        return super.onCreateView(inflater, container, savedInstanceState);
    }
    @Override
    public void onResume() {
        super.onResume();
        LockKeyManager.curPageLockChange();
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(!configIsPassLockKey()) {

            LockKeyManager.removePage(isLockRun);
            LockKeyManager.curPageLockChange();
        }
    }



}
